/**
 * Created by Kpeng on 2017/7/10.
 * Depending on the jquery-1.11.3.min.js.
 * Depending on the style.css.
 * To cite this document, always state the source as shown above.
 */

$(function () {
  wzERP.Companies.init();
});

wzERP.Companies.init = function () {
  wzERP.Companies.addHandle(); //添加公司
  wzERP.Companies.viewHandle(); //查看公司
  wzERP.Companies.editHandle(); //编辑公司
  wzERP.Companies.delHandle(); //删除公司
  wzERP.Common.checkAllTr('.selecte-all');    //全选
  wzERP.Companies.searchHandle();
  wzERP.Companies.turnPage();
};

//添加公司
wzERP.Companies.addHandle = function () {
  var opt = {
    ele: '.addBtn',
    modalBox: '#modal',
    modalTitleStr: '添加公司',
    onApprove: function () {
      if (!!wzERP.Companies.checkform('#company')) {
        layer.msg('填写项不符合规则', {icon: 2});
        return false;
      }
      //获取省市区控件的值
      var temp = wzERP.Common.getCityData('#city1');
      wzERP.Common.saveHandle('#company', '#submitCompanyUrl',
          $(this).find('.setId'), temp);
      wzERP.Common.delayReload(wzERP.delayTime);
    },
    snippets: $("#addCompanyHtml").val()
  };
  wzERP.Common.handleData(opt);
};

//查看公司
wzERP.Companies.viewHandle = function () {
  var opt = {
    ele: '.viewBtn',
    modalTitleStr: '公司详情',
    modalBox: '#modal',
    onApprove: function () {
    },
    snippets: $("#companyFindHtml").val()
  };
  wzERP.Common.handleData(opt);
};

//编辑公司
wzERP.Companies.editHandle = function () {
  var opt = {
    ele: '.editBtn',
    modalBox: '#modal',
    modalTitleStr: '编辑公司',
    onApprove: function () {
      if (!!wzERP.Companies.checkform('#company')) {
        layer.msg('填写项不符合规则', {icon: 2});
        return false;
      }
      //获取省市区控件的值
      var city = wzERP.Common.getCityData('#city1');
      wzERP.Common.saveHandle('#company', '#updateCompanyUrl',
          $(this).find('.setId'), city);
      wzERP.Common.delayReload(wzERP.delayTime);
    },
    snippets: $("#companyUpdateHtml").val()
  };
  wzERP.Common.handleData(opt);
};

//删除公司
wzERP.Companies.delHandle = function () {
  /*var opt = {
   ele: '.delBtn',
   modalBox: '#modal',
   modalTitleStr: '',
   onApprove: function () {
   wzERP.Common.deleteData('#delCompanyUrl', $(this).find('.setId'));
   },
   snippets: $("#employeeDeleteHtml").val()
   };
   wzERP.Common.handleData(opt);*/

  $('#main').on('click', '.delBtn', function () {
    var $self = $(this);
    layer.alert('您确定要删除该数据?', {
          title: '提示信息',
          btn: ['取消', '确认'],
          btn1: function (index) {
            layer.close(index)
          },
          btn2: function () {
            wzERP.Common.deleteData('#delCompanyUrl', $self)
          }
        }
    );
  })
};

//分页跳转
wzERP.Companies.turnPage = function () {
  var opt = {
    pageUrl: $("#companyPageUrl").val(),
    pageSearch: "#searchForm",
    pagesTotal: parseInt($('#pages').val()),
    pageCurr: parseInt($('#current').val()),
    pageSize: parseInt($('#pageSize').val()),
    currId: "#current"
  };
  wzERP.Common.pagingHandle(opt);
};

//条件搜索
wzERP.Companies.searchHandle = function () {
  $("#queryAll").on('click', function () {
    $('#current').val(1);//点击查询默认查第1页,
    var cityData = wzERP.Common.getCityData('#city2');
    if (cityData) {
      $("#provinceCode").val(cityData[1].value);
      $("#countyCode").val(cityData[3].value);
      $("#cityCode").val(cityData[2].value);
    }
    $("#searchForm").submit();
  })
};

//表单验证器
wzERP.Companies.verifier = function (_selector) {
  /*Arg Guide
   @_selector: 对象选择器
   */
  $(_selector).form({
    on: 'blur',
    // inline: true,
    transition: 'fade',
    fields: {
      //公司简称
      companyAbbreviation: {
        identifier: 'companyAbbreviation',
        rules: [{
          type: 'empty',
          prompt: '请输入公司简称'
        },
          {
            type: 'regExp[/^.{0,10}$/]',
            prompt: '字数限制10字'
          }]
      },
      //公司编码
      companyCode: {
        identifier: 'companyCode',
        rules: [{
          type: 'empty',
          prompt: '请输入公司编码'
        },
          {
            type: 'regExp[/^\\d{0,3}$/]',
            prompt: '由三位数字组成'
          }]
      },
      //公司名称
      companyName: {
        identifier: 'companyName',
        rules: [{
          type: 'empty',
          prompt: '请输入公司名称'
        },
          {
            type: 'regExp[/^.{0,20}$/]',
            prompt: '字数限制20字'
          }]
      },
      //注册地址
      registAddr: {
        identifier: 'registAddr',
        rules: [{
          type: 'empty',
          prompt: '请输入公司注册地址'
        },
          {
            type: 'regExp[/^.{0,50}$/]',
            prompt: '字数限制50字'
          }]
      },
      //区域
      codes: {
        identifier: 'codes'
      },
      //详细地址
      detailedAddr: {
        identifier: 'detailedAddr',
        rules: [{
          type: 'empty',
          prompt: '请输入公司详细地址'
        },
          {
            type: 'regExp[/^.{0,50}$/]',
            prompt: '字数限制50字'
          }]
      },
      //邮箱
      email: {
        identifier: 'email',
        optional: true,
        rules: [{
          type: 'regExp[/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/]',
          prompt: '请输入正确的格式'
        },
          {
            type: 'regExp[/^.{0,30}$/]',
            prompt: '限制30字'
          }
        ]
      },
      //联系电话
      phone: {
        identifier: 'phone',
        rules: [{
          type: 'empty',
          prompt: '请输入公司联系电话'
        },
          {
            type: 'regExp[/^[0-9-]{0,15}$/]',
            prompt: '数字及-，字数限制15字\n'
          }]
      },
      //传真
      companyFax: {
        identifier: 'companyFax',
        rules: [{
          type: 'regExp[/^[0-9\-]{0,15}$/]',
          prompt: '数字及-，字数限制15字'
        }]
      },
      //邮编
      zipCode: {
        identifier: 'zipCode',
        rules: [{
          type: 'regExp[/^\\d{0,10}$/]',
          prompt: '数字字数限制10位'
        }]
      },
      //开票抬头
      headerName: {
        identifier: 'headerName',
        rules: [{
          type: 'empty',
          prompt: '请输入开票抬头'
        },
          {
            type: 'regExp[/^.{0,20}$/]',
            prompt: '字数限制20字'
          }]
      },
      //纳税识别号
      taxCode: {
        identifier: 'taxCode',
        rules: [{
          type: 'empty',
          prompt: '请输入纳税识别号'
        }]
      },
    }
  });
};

//上传pdf
wzERP.Companies.uploadFilePdf = function (body_ele, ele_trigger, ele_form) {
  /*Arg Guide
   @ trigger_id 触发按钮id(label)
   @ form_id 表单提交的id
   */
  $(body_ele).on('click', ele_trigger, function () {
    var $self = $(this);
    var input_name = $(this).attr('for');
    var $input_file = $('#' + input_name); //获取上传文件的input
    var maxSize = 2 * 1024 * 1024;//最大容量
    $input_file.off('change').change(function () {
      var $iVal = $input_file.val();
      if ($iVal) {
        if (!$iVal.endsWith('pdf')) {
          layer.msg('请上传pdf格式', {icon: 5})
        } else {
          if ($input_file[0].files[0].size > maxSize) {
            layer.msg('上传pdf不能超过2M', {icon: 5});
            return false;
          }
          $(ele_form).ajaxSubmit({
            type: 'POST',
            success: function (result) {
              if (result.code == 0) {
                var filePath = result.data.filePath;
                var fileName = result.data.fileName;
                $self.text('重新上传');
                $self.siblings('a').remove('.seePdf')
                $self.parent().prepend(
                    '<a class="ui blue button seePdf" href="' + filePath
                    + '" style="display:inline-block" target="_blank">预览</a>');
                $self.siblings('input[type="hidden"]').val(filePath);
                $input_file.val('');
                layer.msg('上传成功!', {icon: 7});
              }
            }
          });
        }
      }
    })
  })
}

//表单验证出口
wzERP.Companies.checkform = function (ele) {
  /*Arg Guide
   @ ele 要验证的表单
   */
  $(ele).form('submit');
  return $(ele).hasClass('error');
}




